package com.zhanggm.zyk.order.controller;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.AlipayConfig;
import com.alipay.api.domain.AlipayTradePagePayModel;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.FileItem;
import java.util.Base64;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/pay/")
public class PayController {

    @GetMapping("pay")
    public String pay(String orderId, String price) throws AlipayApiException {
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCJKkN8T7otSL6FHXKabFxZLd+NEz/YfIrKtfFvSPS1xy89LUyDEah9ucMNXSSPaTifzzZ55XTEOSpAwf/2ESbxfL+7FB8iMvrVOxmAXmNuLGw9RK4CZgDmY0uueqgFJO32CN673nx5d5Iyd4IFFCmnmORyddQl0UpNiSp0TpC7AtIQduxFuD79uWchDEDmA+h2aJdoMNTbi4HNrXkBuVnUSEz2a9541XFnRWGlnoeFtoPuYmLc0TLRHYsNL37vT4Yb5hXjv+KNLDaB2JTWlkqHdiUAJaQMi97rzLAEh4HyYE7H7f4p9q52ELwn1oEX+RemNdWyj2VzOyeZo57eSZD3AgMBAAECggEAWEhIeUlDVgQdA8ZJTdTytz9MRBugmzDFj+T8yRiW0CysXd67AR0raghW31IUxuz9UfZnZJtnxSfZ5yY2HWU6wHD/YmR4CWXwha9tIW8e5flXjTOYH9kROTetrVYqsGztb86TtS1kxjVMdojuuWAZ01IXrwfhjJmhDjh7h7JNet9DQlhhmM/aiCPqlxdOulNwcUVqy7ITL8QTcHUz3AxDE1s/o1Zs8eqvE9w7E6r5pPp71g0U4BtXhtSbnP4XdRVnpo9KVG/tAfMqrdaU+CLmEMB8+i9+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YFX31Z28/IaruAvuas8LgKUAhXpA33mmJChvrevfOiXTFVh+bepc1s0ED34+cFMsnkGLyJvvzIQWqoaet4XgR+4+x6mE885wJCpFqjFNXAUPrT5l5hE5m3hMTp+nIya2qogNaITIlR6Liuj6r9voRTLTZUm48umc7YhP6G9AJzu15vvjGkyNCx4u5M49KS61P13FRsIwYmAtMZPZ41u+ZoBcGanIGwRwghWSSPR/0GtAjExy8/zzaAAQATfthDQIDAQAB";
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi-sandbox.dl.alipaydev.com/gateway.do");
        alipayConfig.setAppId("9021000137666570");
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setFormat("json");
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        AlipayClient alipayClient = new DefaultAlipayClient(alipayConfig);
        AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
        AlipayTradePagePayModel model = new AlipayTradePagePayModel();
        model.setOutTradeNo(orderId);
        model.setTotalAmount(price);
        model.setSubject("Iphone6 16G");
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        request.setBizModel(model);
        request.setReturnUrl("http://localhost:8100");
        AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "POST");
        // 如果需要返回GET请求，请使用
        // AlipayTradePagePayResponse response = alipayClient.pageExecute(request, "GET");
        String pageRedirectionData = response.getBody();
        System.out.println(pageRedirectionData);
        if (response.isSuccess()) {
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return pageRedirectionData;

    }

}
